#!/usr/bin/env bash

icons=("" "" "" "" "" "" "" "")

gettime() {
	# FULL=$(cat /sys/class/power_supply/*/uevent | grep 'POWER_SUPPLY_CHARGE_FULL=' | cut -d '=' -f2)
	# NOW=$(cat /sys/class/power_supply/*/uevent | grep 'POWER_SUPPLY_CHARGE_NOW=' | cut -d '=' -f2)
	# RATE=$(cat /sys/class/power_supply/*/uevent | grep 'POWER_SUPPLY_VOLTAGE_NOW=' | cut -d '=' -f2)

	if [ "$RATE" -gt 0 ]; then
		if [ "$STATE" = "Discharging" ]; then
			upower -e | grep battery | while read line; do upower -i $line; done | grep 'time to empty:' | cut -d ':' -f2 | sed 's/ //g' | sed 's/ours//g' | sed 's/inutes//g'
		else
			upower -e | grep battery | while read line; do upower -i $line; done | grep 'time to full:' | cut -d ':' -f2 | sed 's/ //g' | sed 's/ours//g' | sed 's/inutes//g'
		fi
		date -u -d@"$(bc -l <<< "$EX * 3600")" +%H:%M
	fi
}

geticon() {
	if [ "$STATE" = "Charging" ]; then
    level=$(awk -v n="$CAPACITY" 'BEGIN{print int((n-1)/12)}')
		echo "${icons[$level]}"
	else
    level=$(awk -v n="$CAPACITY" 'BEGIN{print int((n-1)/12)}')
		echo "${icons[$level]}"
	fi
}

status() {
	if [ "$STATE" = "Charging" ]; then
		if [ "$RATE" -gt 0 ]; then
			echo "$(gettime) to full"
		else
			echo ""
		fi
	elif [ "$STATE" = "Discharging" ]; then
		echo "$(gettime) left"
	else
		echo "fully charged"
	fi
}

statch() {
	if [ "$STATE" = "Discharging" ]; then #Not charging, below 20%
		if [ "$CAPACITY" -le 5 ]; then
			echo 'deadly'
		elif [ "$CAPACITY" -le 10 ]; then
			echo 'critical'
		elif [ "$CAPACITY" -le 30 ]; then
			echo 'low'
		else 
			echo 'normal'
		fi
	else 
		echo 'charging'
	fi
}

chargestatus() {
	if [ "$STATE" = "Charging" ]; then
		echo -n ""
	elif [ "$STATE" = "Discharging" ]; then
		echo -n ""
	fi
}

circolor() {
	if [[ "$CAPACITY" -le 20 && "$STATE" = "Discharging" ]]; then
		cat css/_iconcolor.txt | head -1
	else
		cat css/_iconcolor.txt | head -1
	fi
}

cirbgcolor() {
	if [[ "$CAPACITY" -le 20 && "$STATE" = "Discharging" ]]; then
		echo '#EF738A'
	else
		echo 'transparent'
	fi
}

color() {
	if [ "$CAPACITY" -le 20 ]; then
		echo '#EF738A'
	else
		echo 'white'
	fi
}

bgcolor() {
	if [ "$CAPACITY" -le 20 ]; then
		echo '#EF738A'
	else
		echo 'transparent'
	fi
}

wattage() {
	echo "$(bc -l <<< "scale=1; $RATE / 1000000") W"
}

warnedlow=0

while true; do
	RATE=$(cat /sys/class/power_supply/*/voltage_now | head -1)
	CAPACITY=$(cat /sys/class/power_supply/*/capacity | head -1)
	STATE=$(cat /sys/class/power_supply/*/status | head -1)

	if [[ "$CAPACITY" -le 5 && "$STATE" = "Discharging" ]]; then
		if [ $warnedlow == 0 ]; then 
			warnedlow=1
			notify-send 'FREAKING PLUG IN THE CHARGER ALREADY' '!!111!!1!123!!!' -u critical -a 'eww'
		fi
	elif [[ "$CAPACITY" -le 10 && "$STATE" = "Discharging" ]]; then
		if [ $warnedlow == 0 ]; then 
			warnedlow=1
			notify-send 'Battery verrry low' 'Hello?' -u critical -a 'eww'
		fi
	elif [[ "$CAPACITY" -le 20 && "$STATE" = "Discharging" ]]; then
		if [ $warnedlow == 0 ]; then 
			warnedlow=1
			notify-send 'Battery low' 'Plug in your charger pweeeeaaase :>' -u critical -a 'eww'
		fi
	else
	    warnedlow=0
	fi

	echo '{"quickicon": "'"$(chargestatus)"'", "percentage": '"$CAPACITY"', "status": "'"$(status)"'"}'
	sleep 3
done